/* 
 * jxListFiles Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * GPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxListFiles.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jxSwing.component.table.annotation.Column;
import org.jxUtils.file.FileSize;
import org.jxUtils.file.JXFile;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxListFiles
 * @package org.jxListFiles.model
 * @date 01/04/2012 - 17:27:55
 */
public final class MyFile implements Serializable {
	
	private static final long serialVersionUID = 1687037797669516263L;
	
	@Column(name = "Nome do Arquivo")
	private String name;
	
	@Column(name = "Última Modificação", mask = "dd/MM/yyyy HH:mm:ss")
	private Date lastModification;
	
	@Column(name = "Tamanho")
	private FileSize size;
	
	@Column(name = "Diretório")
	private String directory;
	
	/**
	 * @param name
	 * @param directory
	 * @param size
	 * @param lastModification
	 */
	public MyFile(String name, String directory, FileSize size, Date lastModification) {
	
		this.name = name;
		this.directory = directory;
		this.size = size;
		this.lastModification = lastModification;
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
	
		return name;
	}
	
	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
	
		this.name = name;
	}
	
	/**
	 * @return the lastModification
	 */
	public Date getLastModification() {
	
		return lastModification;
	}
	
	/**
	 * @param lastModification
	 *            the lastModification to set
	 */
	public void setLastModification(Date lastModification) {
	
		this.lastModification = lastModification;
	}
	
	/**
	 * @return the size
	 */
	public FileSize getSize() {
	
		return size;
	}
	
	/**
	 * @param size
	 *            the size to set
	 */
	public void setSize(FileSize size) {
	
		this.size = size;
	}
	
	/**
	 * @return the directory
	 */
	public String getDirectory() {
	
		return directory;
	}
	
	/**
	 * @param directory
	 *            the directory to set
	 */
	public void setDirectory(String directory) {
	
		this.directory = directory;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
	
		final int prime = 31;
		int result = 1;
		result = prime * result + ((directory == null) ? 0 : directory.hashCode());
		result = prime * result + ((lastModification == null) ? 0 : lastModification.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((size == null) ? 0 : size.hashCode());
		return result;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
	
		if(this == obj) {
			return true;
		}
		if(obj == null) {
			return false;
		}
		if( !(obj instanceof MyFile)) {
			return false;
		}
		MyFile other = (MyFile)obj;
		if(directory == null) {
			if(other.directory != null) {
				return false;
			}
		}else if( !directory.equals(other.directory)) {
			return false;
		}
		if(lastModification == null) {
			if(other.lastModification != null) {
				return false;
			}
		}else if( !lastModification.equals(other.lastModification)) {
			return false;
		}
		if(name == null) {
			if(other.name != null) {
				return false;
			}
		}else if( !name.equals(other.name)) {
			return false;
		}
		if(size == null) {
			if(other.size != null) {
				return false;
			}
		}else if( !size.equals(other.size)) {
			return false;
		}
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
	
		// TODO: Implementar Internacionalização
		return 
			"Name: " + name + "\n" + 
			"Directory: " + directory + "\n" + 
			"Size: " + size + "\n" + 
			"Last Modification: " + lastModification + "\n\n";
	}
	
	/**
	 * @param jxFile
	 * @return myFile
	 */
	public static MyFile toMyFile(JXFile jxFile) {
	
		return new MyFile(jxFile.getName(), jxFile.getParentFile().getAbsolutePath(), jxFile.getSize(), jxFile.getLastModified());
	}
	
	/**
	 * @param list
	 */
	public static List<MyFile> toMyFileList(List<JXFile> jxFileList) {
	
		List<MyFile> list = new ArrayList<>();
		
		synchronized(jxFileList) {
			
			// 
			for(JXFile jxFile : jxFileList) {
				
				list.add(toMyFile(jxFile));
			}
		}
		
		return list;
	}
}